home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / docs / service.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  11.4 KB  |  264 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''DocsService extends the GDataService to streamline Google Documents
  5.   operations.
  6.  
  7.   DocsService: Provides methods to query feeds and manipulate items.
  8.                     Extends GDataService.
  9.  
  10.   DocumentQuery: Queries a Google Document list feed.
  11. '''
  12. __author__ = 'api.jfisher (Jeff Fisher)'
  13. import urllib
  14. import atom
  15. import gdata.service as gdata
  16. import gdata.docs as gdata
  17. DATA_KIND_SCHEME = 'http://schemas.google.com/g/2005#kind'
  18. DOCUMENT_KIND_TERM = 'http://schemas.google.com/docs/2007#document'
  19. SPREADSHEET_KIND_TERM = 'http://schemas.google.com/docs/2007#spreadsheet'
  20. PRESENTATION_KIND_TERM = 'http://schemas.google.com/docs/2007#presentation'
  21. SUPPORTED_FILETYPES = {
  22.     'CSV': 'text/csv',
  23.     'TSV': 'text/tab-separated-values',
  24.     'TAB': 'text/tab-separated-values',
  25.     'DOC': 'application/msword',
  26.     'ODS': 'application/x-vnd.oasis.opendocument.spreadsheet',
  27.     'ODT': 'application/vnd.oasis.opendocument.text',
  28.     'RTF': 'application/rtf',
  29.     'SXW': 'application/vnd.sun.xml.writer',
  30.     'TXT': 'text/plain',
  31.     'XLS': 'application/vnd.ms-excel',
  32.     'PPT': 'application/vnd.ms-powerpoint',
  33.     'PPS': 'application/vnd.ms-powerpoint',
  34.     'HTM': 'text/html',
  35.     'HTML': 'text/html' }
  36.  
  37. class DocsService(gdata.service.GDataService):
  38.     '''Client extension for the Google Documents service Document List feed.'''
  39.     
  40.     def __init__(self, email = None, password = None, source = None, server = 'docs.google.com', additional_headers = None, **kwargs):
  41.         """Creates a client for the Google Documents service.
  42.  
  43.     Args:
  44.       email: string (optional) The user's email address, used for
  45.           authentication.
  46.       password: string (optional) The user's password.
  47.       source: string (optional) The name of the user's application.
  48.       server: string (optional) The name of the server to which a connection
  49.           will be opened. Default value: 'docs.google.com'.
  50.       **kwargs: The other parameters to pass to gdata.service.GDataService
  51.           constructor.
  52.     """
  53.         gdata.service.GDataService.__init__(self, email = email, password = password, service = 'writely', source = source, server = server, additional_headers = additional_headers, **kwargs)
  54.  
  55.     
  56.     def Query(self, uri, converter = gdata.docs.DocumentListFeedFromString):
  57.         '''Queries the Document List feed and returns the resulting feed of
  58.        entries.
  59.  
  60.     Args:
  61.       uri: string The full URI to be queried. This can contain query
  62.            parameters, a hostname, or simply the relative path to a Document
  63.            List feed. The DocumentQuery object is useful when constructing
  64.            query parameters.
  65.       converter: func (optional) A function which will be executed on the
  66.                  retrieved item, generally to render it into a Python object.
  67.                  By default the DocumentListFeedFromString function is used to
  68.                  return a DocumentListFeed object. This is because most feed
  69.                  queries will result in a feed and not a single entry.
  70.     '''
  71.         return self.Get(uri, converter = converter)
  72.  
  73.     
  74.     def QueryDocumentListFeed(self, uri):
  75.         '''Retrieves a DocumentListFeed by retrieving a URI based off the Document
  76.        List feed, including any query parameters. A DocumentQuery object can
  77.        be used to construct these parameters.
  78.  
  79.     Args:
  80.       uri: string The URI of the feed being retrieved possibly with query
  81.            parameters.
  82.  
  83.     Returns:
  84.       A DocumentListFeed object representing the feed returned by the server.
  85.     '''
  86.         return self.Get(uri, converter = gdata.docs.DocumentListFeedFromString)
  87.  
  88.     
  89.     def GetDocumentListEntry(self, uri):
  90.         '''Retrieves a particular DocumentListEntry by its unique URI.
  91.  
  92.     Args:
  93.       uri: string The unique URI of an entry in a Document List feed.
  94.  
  95.     Returns:
  96.       A DocumentListEntry object representing the retrieved entry.
  97.       '''
  98.         return self.Get(uri, converter = gdata.docs.DocumentListEntryFromString)
  99.  
  100.     
  101.     def GetDocumentListFeed(self):
  102.         """Retrieves a feed containing all of a user's documents."""
  103.         q = gdata.docs.service.DocumentQuery()
  104.         return self.QueryDocumentListFeed(q.ToUri())
  105.  
  106.     
  107.     def UploadPresentation(self, media_source, title):
  108.         '''Uploads a presentation inside of a MediaSource object to the Document
  109.        List feed with the given title.
  110.  
  111.     Args:
  112.       media_source: MediaSource The MediaSource object containing a
  113.           presentation file to be uploaded.
  114.       title: string The title of the presentation on the server after being
  115.           uploaded.
  116.  
  117.     Returns:
  118.       A GDataEntry containing information about the presentation created on the
  119.       Google Documents service.
  120.     '''
  121.         category = atom.Category(scheme = DATA_KIND_SCHEME, term = PRESENTATION_KIND_TERM)
  122.         return self._UploadFile(media_source, title, category)
  123.  
  124.     
  125.     def UploadSpreadsheet(self, media_source, title):
  126.         '''Uploads a spreadsheet inside of a MediaSource object to the Document
  127.        List feed with the given title.
  128.  
  129.     Args:
  130.       media_source: MediaSource The MediaSource object containing a spreadsheet
  131.                     file to be uploaded.
  132.       title: string The title of the spreadsheet on the server after being
  133.              uploaded.
  134.  
  135.     Returns:
  136.       A GDataEntry containing information about the spreadsheet created on the
  137.       Google Documents service.
  138.     '''
  139.         category = atom.Category(scheme = DATA_KIND_SCHEME, term = SPREADSHEET_KIND_TERM)
  140.         return self._UploadFile(media_source, title, category)
  141.  
  142.     
  143.     def UploadDocument(self, media_source, title):
  144.         '''Uploads a document inside of a MediaSource object to the Document List
  145.        feed with the given title.
  146.  
  147.     Args:
  148.       media_source: MediaSource The gdata.MediaSource object containing a
  149.                     document file to be uploaded.
  150.       title: string The title of the document on the server after being
  151.              uploaded.
  152.  
  153.     Returns:
  154.       A GDataEntry containing information about the document created on the
  155.       Google Documents service.
  156.     '''
  157.         category = atom.Category(scheme = DATA_KIND_SCHEME, term = DOCUMENT_KIND_TERM)
  158.         return self._UploadFile(media_source, title, category)
  159.  
  160.     
  161.     def _UploadFile(self, media_source, title, category):
  162.         '''Uploads a file to the Document List feed.
  163.     
  164.     Args:
  165.       media_source: A gdata.MediaSource object containing the file to be
  166.                     uploaded.
  167.       title: string The title of the document on the server after being
  168.              uploaded.
  169.       category: An atom.Category object specifying the appropriate document
  170.                 type
  171.     Returns:
  172.       A GDataEntry containing information about the document created on
  173.       the Google Documents service.
  174.      '''
  175.         media_entry = gdata.GDataEntry()
  176.         media_entry.title = atom.Title(text = title)
  177.         media_entry.category.append(category)
  178.         media_entry = self.Post(media_entry, '/feeds/documents/private/full', media_source = media_source, extra_headers = {
  179.             'Slug': media_source.file_name })
  180.         return media_entry
  181.  
  182.  
  183.  
  184. class DocumentQuery(gdata.service.Query):
  185.     '''Object used to construct a URI to query the Google Document List feed'''
  186.     
  187.     def __init__(self, feed = '/feeds/documents', visibility = 'private', projection = 'full', text_query = None, params = None, categories = None):
  188.         """Constructor for Document List Query
  189.  
  190.     Args:
  191.       feed: string (optional) The path for the feed. (e.g. '/feeds/documents')
  192.       visibility: string (optional) The visibility chosen for the current feed.
  193.       projection: string (optional) The projection chosen for the current feed.
  194.       text_query: string (optional) The contents of the q query parameter. This
  195.                   string is URL escaped upon conversion to a URI.
  196.       params: dict (optional) Parameter value string pairs which become URL
  197.               params when translated to a URI. These parameters are added to
  198.               the query's items.
  199.       categories: list (optional) List of category strings which should be
  200.               included as query categories. See gdata.service.Query for
  201.               additional documentation.
  202.  
  203.     Yields:
  204.       A DocumentQuery object used to construct a URI based on the Document
  205.       List feed.
  206.     """
  207.         self.visibility = visibility
  208.         self.projection = projection
  209.         gdata.service.Query.__init__(self, feed, text_query, params, categories)
  210.  
  211.     
  212.     def ToUri(self):
  213.         '''Generates a URI from the query parameters set in the object.
  214.  
  215.     Returns:
  216.       A string containing the URI used to retrieve entries from the Document
  217.       List feed.
  218.     '''
  219.         old_feed = self.feed
  220.         self.feed = '/'.join([
  221.             old_feed,
  222.             self.visibility,
  223.             self.projection])
  224.         new_feed = gdata.service.Query.ToUri(self)
  225.         self.feed = old_feed
  226.         return new_feed
  227.  
  228.     
  229.     def AddNamedFolder(self, email, folder_name):
  230.         '''Adds a named folder category, qualified by a schema.
  231.  
  232.     This function lets you query for documents that are contained inside a
  233.     named folder without fear of collision with other categories.
  234.  
  235.     Args:
  236.       email: string The email of the user who owns the folder.
  237.       folder_name: string The name of the folder.
  238.  
  239.       Returns:
  240.         The string of the category that was added to the object.
  241.     '''
  242.         category = '{http://schemas.google.com/docs/2007/folders/'
  243.         category += email + '}' + folder_name
  244.         self.categories.append(category)
  245.         return category
  246.  
  247.     
  248.     def RemoveNamedFolder(self, email, folder_name):
  249.         '''Removes a named folder category, qualified by a schema.
  250.  
  251.     Args:
  252.       email: string The email of the user who owns the folder.
  253.       folder_name: string The name of the folder.
  254.  
  255.       Returns:
  256.         The string of the category that was removed to the object.
  257.     '''
  258.         category = '{http://schemas.google.com/docs/2007/folders/'
  259.         category += email + '}' + folder_name
  260.         self.categories.remove(category)
  261.         return category
  262.  
  263.  
  264.